package algor

import "sync"

func NewQueueInt(arr ...int) *QueueInt {
	return &QueueInt{arr: arr}
}

type QueueInt struct {
	arr  []int
	lock sync.RWMutex
}

func (q *QueueInt) Len() int {
	return len(q.arr)
}

func (q *QueueInt) Push(i ...int) {
	q.lock.Lock()
	q.arr = append(q.arr, i...)
	q.lock.Unlock()
}

func (q *QueueInt) Pull() *int {
	q.lock.Lock()
	if q.Len() == 0 {
		return nil
	}

	v := q.arr[0]
	q.arr = q.arr[1:]
	q.lock.Unlock()
	return &v
}

func NewQueueNTreeNode(arr ...*NTreeNode) *QueueNTreeNode {
	return &QueueNTreeNode{arr: arr}
}

type QueueNTreeNode struct {
	arr  []*NTreeNode
	lock sync.RWMutex
}

func (q *QueueNTreeNode) Len() int {
	return len(q.arr)
}

func (q *QueueNTreeNode) Push(i ...*NTreeNode) {
	q.lock.Lock()
	q.arr = append(q.arr, i...)
	q.lock.Unlock()
}

func (q *QueueNTreeNode) Pull() *NTreeNode {
	q.lock.Lock()
	if q.Len() == 0 {
		return nil
	}

	v := q.arr[0]
	q.arr = q.arr[1:]
	q.lock.Unlock()
	return v
}
